package com.skillworld.webapp.model.gamemap;

import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.skillworld.webapp.model.department.Department;
import com.skillworld.webapp.model.item.Item;


@Entity
@org.hibernate.annotations.BatchSize(size=10)
@Table(name="Map")
public class GameMap    // Named as GameMap to avoid confusion with java.util.Map
{
    public static final String MAIN_MAP_NAME = "WORLD"; 
	
	private Long mapId;
	private String name;
	private int lockLevel;
	private Set<Item> requiredItems; // *Some* of these items are required
	private Department department;	 // A map *may* correspond to a department
	private Set<GameMap> links;	     // Maps you can access from this map
	private Position initPosition;
	
	 
	public GameMap() 
	{

	}

	
	public GameMap(String name, int lockLevel)
	{
        super();
        this.name = name;
        this.lockLevel = lockLevel;
    }


	@SequenceGenerator(name="MapIdGenerator",sequenceName="MapSeq")
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO,generator="MapIdGenerator")
	@Column(name="idMap")
	public Long getMapId() {
		return mapId;
	}

	public void setMapId(Long mapId) {
		this.mapId = mapId;
	}
	
	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "lockLevel")
	public int getLockLevel() {
		return lockLevel;
	}

	public void setLockLevel(int lockLevel) {
		this.lockLevel = lockLevel;
	}

    @ManyToMany
    @JoinTable(name="MapItems",
     joinColumns=@JoinColumn(name="idMap"),
     inverseJoinColumns=@JoinColumn(name="idItem")
    )
	public Set<Item> getRequiredItems() {
        return requiredItems;
    }

    protected void setRequiredItems(Set<Item> requiredItems) {
        this.requiredItems = requiredItems;
    }
    
    public void addRequiredItem(Item item)
    {
        getRequiredItems().add(item);
    }
    
    public void removeRequiredItem(Item item)
    {
        getRequiredItems().remove(item);
    }

    @OneToOne(optional=true,mappedBy="gameMap",fetch=FetchType.LAZY)
	public Department getDepartment() {
		return department;
	}

	protected void setDepartment(Department department) {
		this.department = department;
	}
	
	@Transient
	public boolean isDepartment()
	{
	    return getDepartment() != null;
	}

	@ManyToMany
	@JoinTable(name="MapMap",
	 joinColumns=@JoinColumn(name="idMap1"),
	 inverseJoinColumns=@JoinColumn(name="idMap2")
	)
	public Set<GameMap> getLinks() {
		return links;
	}

	protected void setLinks(Set<GameMap> links) {
		this.links = links;
	}

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="x",column=@Column(name="initX")),
        @AttributeOverride(name="y",column=@Column(name="initY"))
        })
    public Position getInitPosition() {
        return initPosition;
    }

    protected void setInitPosition(Position initPosition) {
        this.initPosition = initPosition;
    }
}
